<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form v-if="isDictItem===false" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="字典编码" prop="dictCode">
        <el-input v-model="dataForm.dictCode" placeholder="字典编码"></el-input>
      </el-form-item>
      <el-form-item v-if="isDictItem===false" label="字典名称" prop="dictName">
        <el-input v-model="dataForm.dictName" placeholder="字典名称"></el-input>
      </el-form-item>
    </el-form>
    
    <el-form v-if="isDictItem===true" :model="dataForm" :rules="dataRuleDictItem" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="字典编码" prop="dictCode">
        <el-input v-model="dataForm.dictCode" placeholder="字典编码"></el-input>
      </el-form-item>
      <el-form-item v-if="isDictItem===true" label="字典项编码" prop="dictItemCode">
        <el-input v-model="dataForm.dictItemCode" placeholder="字典项编码"></el-input>
      </el-form-item>
      <el-form-item v-if="isDictItem===true" label="字典项名称" prop="dictItemName">
        <el-input v-model="dataForm.dictItemName" placeholder="字典项名称"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        isDictItem:false,
        visible: false,
        dataForm: {
          id: 0,
          dictCode: '',
          dictName: '',
          dictItemCode: '',
          dictItemName: ''
        },
        dataRule: {
          dictCode: [
            { required: true, message: '字典编码不能为空', trigger: 'blur' }
          ],
          dictName: [
            { required: true, message: '字典名称不能为空', trigger: 'blur' }
          ]
        },
        dataRuleDictItem: {
          dictCode: [
            { required: true, message: '字典编码不能为空', trigger: 'blur' }
          ],
          dictItemCode: [
            { required: true, message: '字典项编码不能为空', trigger: 'blur' }
          ],
          dictItemName: [
            { required: true, message: '字典项名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,flag) {
        this.dataForm.id = id || 0
        this.visible = true
        if(flag==1){
          this.isDictItem=true;
        }else{
          this.isDictItem=false;
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id && this.isDictItem==false) {
            this.$http({
              url: this.$http.adornUrl(`/sys/dict/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              debugger;
              
              if (data && data.code === 0) {
                this.dataForm.dictCode = data.dict.dictCode
                this.dataForm.dictName = data.dict.dictName
              }
            })
          }else if (this.dataForm.id && this.isDictItem==true) {
            this.$http({
              url: this.$http.adornUrl(`/sys/dictItem/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.dictCode = data.dictItem.dictCode
                this.dataForm.dictItemCode = data.dictItem.dictItemCode
                this.dataForm.dictItemName = data.dictItem.dictItemName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if(this.isDictItem){
              this.$http({
                url: this.$http.adornUrl(`/sys/dictItem/${!this.dataForm.id ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData({
                  'id': this.dataForm.id || undefined,
                  'dictCode': this.dataForm.dictCode,
                  'dictItemCode': this.dataForm.dictItemCode,
                  'dictItemName': this.dataForm.dictItemName
                })
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })

            }else{
              this.$http({
                url: this.$http.adornUrl(`/sys/dict/${!this.dataForm.id ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData({
                  'id': this.dataForm.id || undefined,
                  'dictCode': this.dataForm.dictCode,
                  'dictName': this.dataForm.dictName
                })
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })

            }
            
          }
        })
      }
    }
  }
</script>
